package com.docMan.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.ws.rs.FormParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.hibernate.Query;
import org.springframework.stereotype.Service;

import com.docMan.bean.Page;
import com.docMan.bean.PageDangan;
import com.docMan.dao.BaseDao;
import com.docMan.dao.pojo.Dangan;
import com.docMan.service.DangAnFromOaService;
import com.docMan.utils.ProjectUtils;
import com.sun.org.apache.bcel.internal.generic.I2F;

@Service("danganService")
@Path("/oawj")
@Produces(MediaType.APPLICATION_JSON)
public class DangAnFromOaServiceImpl extends BaseServiceImpl<Dangan> implements DangAnFromOaService{
	@Resource(name="danganDao")
	@Override
	public void setDao(BaseDao<Dangan> dao) {
		// TODO Auto-generated method stub
		super.setDao(dao);
	}

	@Override
	@POST
	@Path("/searchOaWj")
	public String searchOaWj(@FormParam("swbh")String swbh,@FormParam("nd")String nd,@FormParam("bt")String bt,@FormParam("sw")String sw) {
		List<Dangan> dangans = null;
		
		String hql="select wj from Dangan wj where";
		List<String> params=new ArrayList<String>();
		if(!swbh.equals("")){
			hql+=" wj.vswbh like ? and";
			params.add('%'+swbh+'%');
		}
		if(!nd.equals("")){
			hql+=" wj.nd = ? and";
			params.add(nd);
		}
		if(!bt.equals("")){
			hql+=" wj.vlwbt like ? and";
			params.add('%'+ bt+'%');
		}
		if(!sw.equals("")){
			hql+=" wj.vlwbh like ?";
			params.add('%'+sw+'%');
		}
		if(hql.endsWith("where")){
			hql=hql.substring(0, hql.indexOf("where"));
		}
		if(hql.endsWith("and")){
			hql=hql.substring(0, hql.lastIndexOf("and"));	
		}
		
		dangans=find(hql+"order by nd desc" ,params);
		
		return ProjectUtils.gson.toJson(dangans);
	}

	@Override
	public Page<Dangan> queryPage(Page<Dangan> pageDangan,String nd,String searchType,boolean isQtSearch,boolean isqtlwdl,String qtlwdw) {
		String hqlString="select da from Dangan da where da.nd=? and da.vlwdw=? order by da.vlastdate "+pageDangan.getOrderByTime();
		String hqlStringqt="select da from Dangan da where da.nd=? and da.vlwdw not in('国土资源部','省国土资源厅','宁波市国土资源局') order by da.vlastdate "+pageDangan.getOrderByTime();
		String hqlNbszfjh="select da from Dangan da where da.nd=? and da.vlwdw in('宁波市人民政府','市委','市政府承办单') order by da.vlastdate "+pageDangan.getOrderByTime();
		if(isqtlwdl){
			hqlStringqt="select da from Dangan da where da.nd=? and da.vlwdw =?  order by da.vlastdate "+pageDangan.getOrderByTime();
		}
		if(searchType.equals("宁波市政府集合")){
			hqlString=hqlNbszfjh;
		}
		Query query;
		if(isQtSearch){
			query=getDao().getQuery(hqlStringqt);
			query.setParameter(0, nd);
			if(isqtlwdl)
				query.setParameter(1, qtlwdw);
		}else if(searchType.equals("宁波市政府集合")){
			query=getDao().getQuery(hqlString);
			query.setParameter(0, nd);
		}else{
			query=getDao().getQuery(hqlString);
			query.setParameter(0, nd);
			query.setParameter(1, searchType);
		}
		pageDangan=newPage(pageDangan, query);
		return pageDangan;
	}
}
